package com.hqyj.controller;


import com.hqyj.dao.UserInfoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Random;


/**
 * 处理 登录请求的 控制器类
 */
@Controller //表示这个类 处理 前端页面的请求
public class UpdateController {

    //创建邮箱工具类
    @Autowired
    JavaMailSender javaMailSender;

    // 创建现实接口UserInfoDao的实现类对象
    @Autowired(required = false)

    UserInfoDao dao;


    //    访问email页面
    //RequestMapping()定义访问的页面地址
    @RequestMapping("sendCode")
    @ResponseBody//处理ajax请求
    public HashMap sendCode(String email, HttpServletRequest request) {
        HashMap<String, Object> map = new HashMap();
        try {
            //创建一个信封
            SimpleMailMessage message = new SimpleMailMessage();
            //定义收件人
            message.setTo(email);
            //定义发件人
            message.setFrom("991171466@qq.com");
            //定义邮件标题
            message.setSubject("来自叶叶叶的验证码");
            //生成一个随机数
            Random rd = new Random();
            int code = rd.nextInt(9999);
            //获取session对象
            HttpSession session = request.getSession();
            //存入验证码到session中
            session.setAttribute("code", code);
            //定义邮件内容
            String conent = "你发送的验证码是：" + code;
            message.setText(conent);
            //发送邮件
            javaMailSender.send(message);

            map.put("info", "验证码发送成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("info", "验证码发送失败");
        }
        return map;
    }

    //    处理登录按钮提交的请求
    @RequestMapping("/updateForm")
    public String emailFrom(String code, Model model, HttpServletRequest request, String email, String userPwd) {
        //创建session对象
        HttpSession session = request.getSession();
        //取出发送到邮箱的验证码
        String myCode = session.getAttribute("code").toString();
        int num1 = dao.Search_username(email); //查询邮箱
        System.out.println("邮箱="+num1);
        if (num1 > 0) {
            int len = userPwd.length();
            System.out.println("邮箱="+len);
            if (len > 6) {
                if (code.equals(myCode)) {
                    int str = dao.Change_email(userPwd,email);
                    model.addAttribute("info", "密码修改成功");
                    return "login";
                } else {
                    model.addAttribute("info", "邮箱验证失败，请重新注册！");
                    return "update";
                }

            } else {
                System.out.println("修改失败 密码长度需大于6");
                //定义要传给前端的提示信息
                model.addAttribute("info", "修改失败 密码长度需大于6");
                return "update";
            }
        } else {
            System.out.println("该用户不存在，请重新输入");
            //定义要传给前端的提示信息
            model.addAttribute("info", "该用户不存在，请重新输入");
            return "update";

        }

    }


}
